
package com.esun.cheetah.view.personal;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalcommunicater.BindingCardCommunicater;

import com.esun.cheetah.biz.personal.personalinfo.RechargeUserInfo;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;


import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
/**
 * 
 * @Package com.esun.cheetah.view.personal.RechargeUninUnbinding
 * @ClassName: RechargeUninUnbinding
 * @Description: TODO(recharge activity)
 * @author handy <br/>
 * @date create at 2012-7-31 下午3:01:38
 *
 */
public class RechargeUninUnbinding extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	private Button btnGoBack;
	private Button btnChooseBank;
	private Button btnSubmit;
	private Button btnProvinceShenzhen;

	private EditText etOtherProvince;
	private EditText etCardNum;
	private Button btnClearCardNum;
//	private Button btnClearaddress;

	private EditText etRealName;
	private Button btnClearRealNam;

	private EditText etIDNum;
	private Button btnClearIDNum;

	private RadioGroup radioBankZone;

	private Dialog dialogChooseBank;

	private String witchBank = "";
	private String bankType = "";

	private BindingCardCommunicater bindingCardCommunicater;

	private String bankZone = "0";
	private String bankAdress = "广东省深圳市";

	private Handler binding_card_handler;

	// private RechargeController controller = new RechargeController();

	private ProgressDialog progressSubmit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
		// WindowManager.LayoutParams. FLAG_FULLSCREEN);
		setContentView(R.layout.bind_cardbinding);
		initPage();
		initHandler();
	}

	private void initPage() {

		btnGoBack = (Button) this
				.findViewById(R.id.recharge_cardbinding_btn_goBack);
		btnGoBack.setOnClickListener(this);

		btnChooseBank = (Button) this
				.findViewById(R.id.recharge_cardbinding_btn_chooseBank);
		btnChooseBank.setOnClickListener(this);

		btnSubmit = (Button) this
				.findViewById(R.id.recharge_cardbinding_btn_submit);
		btnSubmit.setOnClickListener(this);

		btnProvinceShenzhen = (Button) this
				.findViewById(R.id.recharge_cardbinding_btn_shenzhen);
		btnProvinceShenzhen.setOnClickListener(this);

		etOtherProvince = (EditText) this
				.findViewById(R.id.recharge_cardbinding_et_other);
		/*
		 * btnClearaddress = (Button) this
		 * .findViewById(R.id.loginact_btn_clean_cardbinding_et_other);
		 * etOtherProvince = new EditTextInputUitl(etOtherProvince,
		 * btnClearaddress) .getEditText();
		 */
		etOtherProvince.setFocusableInTouchMode(false);
		etOtherProvince.setOnClickListener(this);

		etCardNum = (EditText) this.findViewById(R.id.recharge_binding_cardNum);
		btnClearCardNum = (Button) this
				.findViewById(R.id.recharge_binding_cardNum_btn_claenNew);
		etCardNum = new EditTextInputUitl(etCardNum, btnClearCardNum)
				.getEditText();

		etRealName = (EditText) this
				.findViewById(R.id.recharge_binding_realUserName);
		btnClearRealNam = (Button) this
				.findViewById(R.id.recharge_binding_realUserName_btn_claenNew);
		etRealName = new EditTextInputUitl(etRealName, btnClearRealNam)
				.getEditText();

		etIDNum = (EditText) this.findViewById(R.id.recharge_binding_userIdNum);
		btnClearIDNum = (Button) this
				.findViewById(R.id.recharge_binding_userIdNum_btn_claenNew);
		etIDNum = new EditTextInputUitl(etIDNum, btnClearIDNum).getEditText();

		radioBankZone = (RadioGroup) this
				.findViewById(R.id.recharge_cardbinding_bankProvince);
		radioBankZone.setOnCheckedChangeListener(this);
		radioBankZone.check(R.id.recharge_cardbinding_shenzhen);

		/**
		 * 绑定银行卡界面点击选择银行弹出的选择银行框太大
		 */
		dialogChooseBank = new Dialog(this, R.style.dialog);
		dialogChooseBank.setContentView(R.layout.recharge_banktype_alert);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_jianshe))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_nongye))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_gongshang))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_zhaoshang))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_jiaotong))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_xingye))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_zhongxin))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_huaxia))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_shenfazhan))
				.setOnClickListener(this);
		((Button) dialogChooseBank
				.findViewById(R.id.recharge_banktype_btn_bgBack))
				.setOnClickListener(this);

		progressSubmit = ViewerUtil.showProgressDialog(this, "提交中...");

	}

	private void initHandler() {

		binding_card_handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case Config.COMMUNICATER_SUCCESS:
					// saveSharedPreferences();
					// saveUserInfo();
					// gotoLoggedinAct();
					// closeDialog();
					Intent intent = new Intent(RechargeUninUnbinding.this,
							BankCardBindingAct.class);
					startActivity(intent);
					RechargeUninUnbinding.this.finish();
					RechargeUserInfo.getUserInfo().setCardNum(
							etCardNum.getText().toString());
					progressSubmit.cancel();
					break;
				case Config.COMMUNICATER_FAIL:
//					ViewerUtil.showTipDialog(RechargeUninUnbinding.this,
//							bindingCardCommunicater.getServerMsg(), false);
					Toast.makeText(RechargeUninUnbinding.this, bindingCardCommunicater.getServerMsg(), 500).show();
					EDebug.o("通讯异常1" + bindingCardCommunicater.getServerMsg());
					// closeDialog();
					progressSubmit.cancel();
					break;
				case Config.COMMUNICATER_EXCEPTION:
//					ViewerUtil.showTipDialog(getParent(),
//							bindingCardCommunicater.getServerMsg(), false);
					Toast.makeText(RechargeUninUnbinding.this, bindingCardCommunicater.getServerMsg(), 500).show();
					EDebug.o("通讯异常2" + bindingCardCommunicater.getMsg());
					// closeDialog();
					progressSubmit.cancel();
					break;
				}
			}
		};
	}

	/*
	 * private void initHandler() { handler = new Handler() {
	 * 
	 * @Override public void handleMessage(Message msg) { // TODO Auto-generated
	 * method stub super.handleMessage(msg);
	 * 
	 * progressSubmit.cancel();
	 * 
	 * switch (msg.what) {
	 * 
	 * case BINDING_SUCCESS: showTipDialog(); break;
	 * 
	 * case BINDING_FAILED:
	 * 
	 * ViewerUtil.showTipDialog(RechargeUninUnbinding.this,
	 * bindingCardCommunicater.getMsg(), false);
	 * 
	 * break; }
	 * 
	 * } }; }
	 */

	// private void doBinding(final String cardNum, final String realName, final
	// String ID) {
	// new Thread() {
	//
	// @Override
	// public void run() {
	// // TODO Auto-generated method stub
	// super.run();
	//
	// if (controller.isBindingCardOk(bankType, witchBank, cardNum, realName,
	// ID, bankAdress, bankZone)) {
	// Message msg = handler.obtainMessage(BINDING_SUCCESS);
	// handler.sendMessage(msg);
	// } else {
	// Message msg = handler.obtainMessage(BINDING_FAILED);
	// handler.sendMessage(msg);
	// }
	// }
	// }.start();
	// }

	private void getBankInfo(View v) {
		witchBank = ((Button) v).getText().toString();
		bankType = v.getTag().toString();

		btnChooseBank.setText(witchBank);
		dialogChooseBank.cancel();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.recharge_cardbinding_btn_goBack:
			this.finish();
			break;

		case R.id.recharge_cardbinding_btn_chooseBank:
			dialogChooseBank.show();
			break;

		case R.id.recharge_cardbinding_btn_submit:

			bankAdress = etOtherProvince.getText().toString();

			if (bankType.equals("")) {
//				ViewerUtil.showTipDialog(this, "请选择银行", false);
				Toast.makeText(this, "请选择银行", 500).show();
			}
			// else if () {
			//
			// }
			else if (etCardNum.getText().toString().length() < 1) {
//				ViewerUtil.showTipDialog(this, "请填写卡号", false);
				Toast.makeText(this, "请填写卡号", 500).show();
			} else if (etRealName.getText().toString().length() < 1) {
//				ViewerUtil.showTipDialog(this, "请填写真实姓名", false);
				Toast.makeText(this, "请填写真实姓名", 500).show();
			} else if (etIDNum.getText().toString().length() < 1) {
				ViewerUtil.showTipDialog(this, "请填写身份证号", false);
				Toast.makeText(this, "请填写身份证号", 500).show();
			} else {

				progressSubmit.show();
				bindingCardCommunicater = new BindingCardCommunicater(
						binding_card_handler, bankType, bankZone, etCardNum
								.getText().toString(), etRealName.getText()
								.toString(), etIDNum.getText().toString(),
						bankAdress);
				EDebug.o("etOtherProvin------"
						+ etOtherProvince.getText().toString());
				EDebug.o("kahao--------" + etCardNum.getText().toString());
				EDebug.o("name--------" + etRealName.getText().toString());
				EDebug.o("etIDNum--------" + etIDNum.getText().toString());
				etCardNum.getText().toString();
				etRealName.getText().toString();
				etIDNum.getText().toString();
				bindingCardCommunicater.startCommunicate();
				UserInfo.getUserInfo().getUserpwdMD5();
				EDebug.o("______password md5"
						+ UserInfo.getUserInfo().getUserpwdMD5());
				// doBinding(etCardNum.getText().toString(),
				// etRealName.getText().toString(),
				// etIDNum.getText().toString());
			}

			break;

		case R.id.recharge_cardbinding_btn_shenzhen:

			radioBankZone.check(R.id.recharge_cardbinding_shenzhen);
			// RechargeUserInfo.getUserInfo().setBanckName("深发");

			break;

		case R.id.recharge_cardbinding_et_other:

			radioBankZone.check(R.id.recharge_cardbinding_other);

			break;

		case R.id.recharge_banktype_btn_jianshe:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("建设");
			break;

		case R.id.recharge_banktype_btn_nongye:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("农业");
			break;

		case R.id.recharge_banktype_btn_gongshang:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("工商");
			break;

		case R.id.recharge_banktype_btn_zhaoshang:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("招商");
			break;

		case R.id.recharge_banktype_btn_jiaotong:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("交通");
			break;

		case R.id.recharge_banktype_btn_xingye:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("兴业");
			break;

		case R.id.recharge_banktype_btn_zhongxin:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("中信");
			break;

		case R.id.recharge_banktype_btn_huaxia:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("华夏");
			break;

		case R.id.recharge_banktype_btn_shenfazhan:
			getBankInfo(v);
			RechargeUserInfo.getUserInfo().setBanckName("深发展");
			break;

		case R.id.recharge_banktype_btn_bgBack:
			dialogChooseBank.cancel();
			break;
		}

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {

		case R.id.recharge_cardbinding_shenzhen:
			bankZone = "0";
			bankAdress = "广东省深圳市";

			etOtherProvince.setFocusableInTouchMode(true);
			// btnProvinceShenzhen.setTextColor(Color.parseColor("#583320"));
			// btnProvinceShenzhen.setBackgroundResource(R.drawable.recharge_zhbd_kaihu_edittext_pressed_189x27);
			etCardNum.requestFocus();
			EDebug.o("bankAdress_1--" + bankAdress);
			break;

		case R.id.recharge_cardbinding_other:
			bankZone = "1";
			bankAdress = "";

			etOtherProvince.setFocusableInTouchMode(true);

			etOtherProvince.requestFocus();
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(etOtherProvince.getWindowToken(), 0);
			bankAdress = etOtherProvince.getText().toString();
			EDebug.o("bankAdress_2---" + bankAdress);
			// btnProvinceShenzhen.setTextColor(Color.parseColor("#555555"));
			// btnProvinceShenzhen.setBackgroundResource(R.drawable.recharge_zhbd_kaihu_edittext_disable_189x27);

			break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(),
		// ConnectionChangeReceiver.getIntentFilter());
	}

	@SuppressWarnings("unused")
	private void showTipDialog() {
		final Dialog tipDialog = new Dialog(RechargeUninUnbinding.this,
				R.style.dialog);
		tipDialog.getWindow().setContentView(
				R.layout.viewerutil_dialog_tipwithtitle);
		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.viewerutildialogtipwithtitle_btn_submit:
					tipDialog.dismiss();
					RechargeUninUnbinding.this.finish();
					break;

				default:
					break;
				}
			}
		};
		Button btnSubmit = (Button) tipDialog.getWindow().findViewById(
				R.id.viewerutildialogtipwithtitle_btn_submit);
		btnSubmit.setOnClickListener(listener);
		tipDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				// TODO Auto-generated method stub
				RechargeUninUnbinding.this.finish();
			}
		});
		tipDialog.show();
	}

}
